#pragma once

#include <iostream>
#include <string>
#include <assert.h>
#include <vector>
#include <unordered_map>

#include "../comm/util.hpp"
#include "../comm/log.hpp"

// 根据题目list的文件，加载所有的题目信息到内存中
// model: 主要用来与数据进行交互，对外提供访问数据的接口

namespace ns_model
{
    using namespace ns_log;
    using namespace std;
    struct Question
    {
        std::string number; // 题目的编号
        std::string title;  // 题目的标题
        std::string star;   // 题目的难度: 简单 中等 困难
        int cpu_limit;      // 时间要求(s)
        int mem_limit;      // 空间要求(kb)
        std::string desc;   // 题目的描述
        std::string header; // 提供给用户的编辑代码
        std::string tail;   // 测试用例，需要和header拼接形成完整的代码
    };

    const string questions_list = "./questions/questions.list";
    const string questions_path = "./questions/";

    class Model
    {
    public:
        Model()
        {
            assert(LoadQuestionList(questions_list));
        }
        ~Model()
        {
        }
        bool LoadQuestionList(const string &question_list)
        {
            // 加载配置文件: question/questions.list + 题目编号文件
            ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL) << "加载题库失败,检查是否存在题库文件" << "\n";
                return false;
            }
            string line;
            while (getline(in, line))
            {
                vector<string> vs;
                StringUtil::SplitString(line, &vs, " ");
                if (vs.size() != 5)
                {
                    LOG(WARNING) << "部分题目加载失败，请检查题库文件是否规范" << "\n";
                    continue;
                }
                // 1. 判断回文数 简单  1 3000
                Question q;
                q.number = vs[0];
                q.title = vs[1];
                q.star = vs[2];
                q.cpu_limit = atoi(vs[3].c_str());
                q.mem_limit = atoi(vs[4].c_str());

                string path = questions_path;
                path += q.number;

                FileUtil::ReadFile(path + "/desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path + "/header.cpp", &(q.header), true);
                FileUtil::ReadFile(path + "/tail.cpp", &(q.tail), true);

                questions.insert({q.number, q});
            }
            LOG(INFO) << "加载题库成功" << "\n";
            in.close();
            
            return true;
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            if (questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }
            for (auto &e : questions)
            {
                out->push_back(e.second);
            }
            return true;
        }
        bool GetOneQuestion(const string &number, Question *q)
        {
            const auto &x = questions.find(number);
            if (x == questions.end())
            {
                LOG(ERROR) << "用户获取题目失败，题目编号: " << number << "\n";
                return false;
            }
            *q = x->second;
            return true;
        }

    private:
        // 题号:题目细节
        unordered_map<string, Question> questions;
    };
}
